package com.company;


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by hehe on 2016/10/2.
 */
public class WebService {

    static String sURL="http://139.224.2.217:607/Users/MethodLogin?toUrl=/Knowledge/Index";
    static String responseCookie;
    public String Login(String usr,String pwd) throws IOException {
        StringBuilder sbR = new StringBuilder();

        URL url = new URL(sURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);//允许连接提交信息
        connection.setRequestMethod("POST");//网页默认“GET”提交方式

        StringBuffer sb = new StringBuffer();
        sb.append("username="+usr);
        sb.append("&password="+pwd);
        connection.setRequestProperty("Content-Length",
                String.valueOf(sb.toString().length()));

        OutputStream os = connection.getOutputStream();
        os.write(sb.toString().getBytes());
        os.close();

        //取Cookie
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        responseCookie = connection.getHeaderField("Set-Cookie");//取到所用的Cookie
        System.out.println("cookie:" + responseCookie);

        //取返回的页面
        String line = br.readLine();
        while (line != null) {
            sbR.append(line);
            line = br.readLine();
        }

        return sbR.toString();
    }

    public static String viewPage() throws IOException
    {
        StringBuilder sbR = new StringBuilder();

        //打开URL连接
        URL url1 = new URL("http://139.224.2.217:607/KB/Edit");
        HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();

        //给服务器送登录后的cookie
        connection1.setRequestProperty("Cookie", responseCookie);

        //读取返回的页面信息到br1
        BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));

        //取返回的页面,br1转sbR
        String line1= br1.readLine();
        while (line1 != null) {
            sbR.append(line1);
            line1 = br1.readLine();
        }
        return sbR.toString();
    }
    public String submitForm(Map<String, Object> requestParamsMap) throws IOException{
        StringBuilder sbR = new StringBuilder();


        //打开URL连接
        URL url1 = new URL("http://139.224.2.217:607/KB/Edit");
        HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
        //给服务器送登录后的cookie
        connection1.setRequestProperty("Cookie", responseCookie);
        // //设置连接属性
        connection1.setDoOutput(true);// 使用 URL 连接进行输出
        connection1.setDoInput(true);// 使用 URL 连接进行输入
        connection1.setUseCaches(false);// 忽略缓存
        connection1.setRequestMethod("POST");// 设置URL请求方法
        String requestString = "客服端要以以流方式发送到服务端的数据...";
        // 设置请求属性
// 获得数据字节数据，请求数据流的编码，必须和下面服务器端处理请求流的编码一致
        connection1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection1.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
        connection1.setRequestProperty("Charset", "UTF-8");
        connection1.setRequestProperty("Charset", "UTF-8");
        connection1.setRequestProperty("Host", "139.224.2.217:607");
        connection1.setRequestProperty("Origin", "http://139.224.2.217:607");
        connection1.setRequestProperty("Referer", "http://139.224.2.217:607/KB/Edit");



        StringBuffer params = new StringBuffer();

        Iterator it = requestParamsMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry element = (Map.Entry) it.next();
            params.append(element.getKey());
            params.append("=");
            params.append(element.getValue());
            params.append("&");
        }
        if (params.length() > 0) {
            params.deleteCharAt(params.length() - 1);
        }
        if (params.length() > 0) {
            params.deleteCharAt(params.length() - 1);
        }

        PrintWriter printWriter = null;
        // 获取URLConnection对象对应的输出流
        printWriter = new PrintWriter(connection1.getOutputStream());
        // 发送请求参数
        System.out.println(params.toString());
        printWriter.write(params.toString());
        // flush输出流的缓冲
        printWriter.flush();
        // 根据ResponseCode判断连接是否成功
        //读取返回的页面信息到br1
        BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream()));

        //取返回的页面,br1转sbR
        String line1= br1.readLine();
        while (line1 != null) {
            sbR.append(line1);
            line1 = br1.readLine();
        }

        return sbR.toString();
    }
}
